<?php
global $_RQ,$_AR;
if($_SESSION['os']=='windows'){
	exi('','',SITEROOT.'/manage/index.php?action=user.login');
}
cfc('core')->checklogin();
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return['role']=$_SESSION['role'];
$return['item']=$this->plugin_setting('setting');
if(!$return['item']['price1'] || !$return['item']['start'] || !$return['item']['juli']){
	exi('未设置计费价格，请联系管理员设置','error','/home/');
}
$return['member']=array();
$this->checkmember();
$basic=$this->plugin_setting('setting');
$return['sharedata']=array(
		'title'=>$basic['name'],
		'desc'=>'我分享了一个打的应用，快来看看吧',
		'link'=>custom_url('jy_taxi/index/',true),
		'imgUrl'=>ATTACHMENT_URL.$basic['logo'],
	);
if($_RQ['op']=='main'){	
	$return['uid']=$_SESSION['uid'];
	$return['info']=array();
	$return['show']=0;
	$return['show2']=0;
	$return['province']=file_get_contents('core/resource/area.txt');
	$return['province']=unserialize($return['province']);
	$return['pcd']=array(
		'province' =>trim($_RQ['province']),
		'city' =>trim($_RQ['city']),
		'district' =>trim($_RQ['district']),
	);
	//exi(2,'error');
	$history=pdo_getall('jy_taxi_history',array('uid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'','','createtime desc');
	foreach($history as &$v){
		$v['lat_lng']=json_decode($v['lat_lng'],true);
	}
	$return['history']=$history;
	if($_RQ['lat']){
		$return['info']['lat']=cfc('tools')->Convert_GCJ02_To_BD09($_RQ['lat'],$_RQ['lng']);
	}
	$member=pdo_get('jy_taxi_driver',array('mid'=>$_SESSION['uid']));
	$info=model('core/member')->basic($_SESSION['uid']);
	if(!$member){
		$member=pdo_get('jy_taxi_member',array('mid'=>$_SESSION['uid']));
		if(!$member){
		$member['user']='no';	
		}else{
		$member['user']='member';
		}
	}else{
		$member['user']='driver';
	}
	$member['nickname']=$info['nickname'];
	$member['avatar']=$info['avatar'];
	$member['credit2']=$info['credit2'];
	$return['member']=$member;
	exi($return);
}
if($_RQ['op']=='pirce'){
	$post=get_postdata();
	$setting=$this->plugin_setting('setting');
	$distance=ceil($post['distance']/1000);
	$price=$setting['start'];
	if($distance>$setting['juli']){
		$num=ceil($distance/$setting['juli']);
		$price+=($num-1)*$setting['price1'];
	}
	$res=array(
		'price'=>$price,
	);
	exi($res);
}
if($_RQ['op']=='create'){
	$post=get_postdata();
	$data=array_fields(array('start_seat','dest_seat','juli','time'),$post);
	if(!$data['dest_seat']){
		exi('请选择目的地','error');
	}
	if(!$data['dest_seat']){
		exi('未获取到距离和价格,请刷新重试','error');
	}
	$drivers=pdo_getall('jy_taxi_driver',array('uniacid'=>$_SESSION['uniacid'],'busy'=>1,'status'=>1,'stop'=>0));	
	if(!$drivers){
		//pdo_update('jy_taxi_order',array('status'=>6),array('id'=>$orderid));
		exi('暂无司机,等待司机接单','error');
	}
	$s1=pdo_getall('jy_taxi_order',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'status'=>1));
	$s2=pdo_getall('jy_taxi_order',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'status'=>2));
	if($s1 || $s2){
		exi('您还有未完成的订单，请先完成','error','/order/');
	}
		$dw=substr($data['juli'],-1);
			if($dw=='里'){
				$juli= Float($data['juli']); 
				if($juli > $return['item']['juli']){
				
				$data['price1'] = ($juli-$return['item']['juli'])*$return['item']['price1']+$return['item']['start'];
				}else{
				$data['price1'] = $return['item']['start'];
				}
			}else{
				$data['price1'] = $return['item']['start'];
			}
	
	$lat=$post['lat'];
	$data['lat']=json_encode($post['lat']);
	$data['mid']=$_SESSION['uid'];
	if($post['name']&&$post['mobile']){
		$data['name']=$post['name'];	
		$data['mobile']=$post['mobile'];	
	}else{
		$member=pdo_get('jy_taxi_member',array('mid'=>$_SESSION['uid']));
		$data['name']=$member['name'];	
		$data['mobile']=$member['mobile'];
	}
	$data['ordersn']=get_ordersn('jy_taxi_order','ordersn');
	$data['createtime']=TIMESTAMP;
	$data['uniacid']=$_SESSION['uniacid'];
	//$data['price2']='';
	//$data['price']='';
	//print_r($data);exit();
	pdo_insert('jy_taxi_order',$data);
	$orderid=pdo_insertid();
	
	foreach($drivers as &$v){
		$v['latlng']=json_decode($v['latlng'],true);
		$v['juli']=cfc('tools')->getdistance($post['lat']['lng'],$post['lat']['lat'],$v['latlng']['lng'],$v['latlng']['lat']);
		$v['juli']=intval($v['juli']/1000);
	}
	paixu($drivers,'juli');	
	
	if(intval($return['item']['jkjuli']) < $drivers['0']['juli']){
		$price2=($drivers['0']['juli']-$return['item']['jkjuli'])*$return['item']['price2'];
	}else{
		$price2=0;
	}
	
	$price=$data['price1']+$price2;
	
	//pdo_update('jy_taxi_order',array('driver_id'=>$drivers['0']['id'],'jjuli'=>$drivers['0']['juli'],'price2'=>$price2,'price'=>$price),array('id'=>$orderid));
	pdo_update('jy_taxi_driver',array('busy'=>2),array('id'=>$drivers['0']['id']));
	//$setting=core_setting('cloud');//获取云服务参数
	//生成前端签名
	//$sign=sha1(TIMESTAMP.$setting['cloudkey'].$_SESSION['uniacid'].$_SESSION['uid'].$setting['secret']);
	//拼接前端发送消息的前缀
	//$message_pre1=$sign.';'.TIMESTAMP.';'.$setting['cloudkey'].';'.$_SESSION['uniacid'].';'.$_SESSION['uid'].';';
	$m=array('lat'=>$lat,'orderid'=>$orderid,'time'=>$data['time'],'name'=>$member['name'],'mobile'=>$member['mobile'],'chufa'=>$data['start_seat'],'mudi'=>$data['dest_seat'],'jjuli'=>$drivers['0']['juli'],'juli'=>$data['juli'],'price'=>$price,'price2'=>$price2);
	$message=ijson_encode($m);
	$res=cfc('jinyun_socket')->send2uid($message,$drivers['0']['mid']);
	//print_r($res);exit('success');
	//$post=array(
	//	'message'=>$message_pre1.$drivers['0']['mid'].';'.$message,
	//);
	//cloud_request('cloud.forward',$post);
	exi('已呼叫，请耐心等待司机接单','','/wait/main');
	
}
if($_RQ['op']=='history'){
	$post=get_postdata();
	$address=pdo_get('jy_taxi_history',array('uid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid'],'address'=>$post['address']));
	if($address){
		pdo_update('jy_taxi_history',array('createtime'=>TIMESTAMP),array('id'=>$address['id']));
	}else{
		$data=array('uniacid'=>$_SESSION['uniacid'],'uid'=>$_SESSION['uid'],'createtime'=>TIMESTAMP,'address'=>$post['address'],'pcd'=>$post['pcd'],'lat_lng'=>json_encode($post['lat_lng']));
		pdo_insert('jy_taxi_history',$data);
	}
	exi('success');
}
?>